package com.njwb.day16.dom.util;

import org.w3c.dom.Document;
import org.xml.sax.SAXException;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.io.InputStream;

public class DomUtil {

    //获取需要读取的xml文件
    public static Document getDocumentInstance(String path){
        Document document = null ;
        DocumentBuilder documentBuilder = null ;
        try {
            documentBuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            InputStream is = DomUtil.class.getClassLoader().getResourceAsStream(path);
            document=documentBuilder.parse(is);
        } catch (SAXException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ParserConfigurationException e) {
            e.printStackTrace();
        }
        return document;
    }
}
